//
//  BubbleChatCell.m
//  CustomBubbleChatView
//
//  Created by Lixiaoyong on 13-6-26.
//  Copyright (c) 2013年 Revenco. All rights reserved.
//

#import "BubbleChatCell.h"

#import <QuartzCore/QuartzCore.h>

#import "UIImageView+WebCache.h"
#import "DLog.h"

#define kChatLableFont [UIFont systemFontOfSize:14.0]
#define kChatLableWidth 200.0f
#define kChatLableMinHeight 50.0f
#define kChatHeaderImageHW 42.0f
#define kArrawScale 10.0f
#define kMarginTop 8.0f

#pragma mark - BubbleChatContentHeight
@implementation BubbleChatContentHeight
+ (CGSize)getStringLength:(NSString *)string
{
    UIFont *font = kChatLableFont;
    CGSize size = [string sizeWithFont:font constrainedToSize:CGSizeMake(kChatLableWidth, 1000) lineBreakMode:NSLineBreakByTruncatingTail];
    return size;
}
@end

#pragma mark - BubbleChatView

@implementation BubbleChatView
@synthesize locationFlag;

- (void)dealloc
{
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        
        self.textColor = [UIColor blackColor];
        self.font = kChatLableFont;
        self.contentMode = UIViewContentModeScaleAspectFit;
        self.numberOfLines = 0;
        self.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    UIImage *image = (self.locationFlag == 1) ? [UIImage imageNamed:@"chat_left.png"] : [UIImage imageNamed:@"chat_right.png"];
    image = (self.locationFlag == 1) ? [image stretchableImageWithLeftCapWidth:22 topCapHeight:21] : [image stretchableImageWithLeftCapWidth:22 topCapHeight:23];
    [image drawInRect:CGRectMake(0, rect.origin.y, rect.size.width, rect.size.height)];
    
    CGSize textSize = [BubbleChatContentHeight getStringLength:self.text];
	CGFloat textX = (CGFloat)image.leftCapWidth - 8.0f + ((self.locationFlag == 1) ? rect.origin.x : 0.0);
    CGFloat textY = (rect.size.height - textSize.height) / 2.0;
    CGFloat width = textSize.width;
    CGRect textFrame = CGRectMake(textX,
                                  textY,
                                  width,
                                  rect.size.height);
    
	[self.text drawInRect:textFrame
                 withFont:kChatLableFont
            lineBreakMode:NSLineBreakByTruncatingTail
                alignment: NSTextAlignmentLeft];
}

@end

#pragma mark - BubbleChatCell

@implementation BubbleChatCell
@synthesize headerImage;
@synthesize chatView;
@synthesize indicatorView;

- (void)dealloc
{
    self.headerImage = nil;
    self.chatView = nil;
    self.indicatorView = nil;
    [super dealloc];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        CGRect rect = CGRectZero;
        self.headerImage = [[[UIImageView alloc] initWithFrame:rect] autorelease];
        self.headerImage.backgroundColor = [UIColor clearColor];
        [self addSubview:self.headerImage];
        
        self.chatView = [[[BubbleChatView alloc] initWithFrame:rect] autorelease];
        [self addSubview:self.chatView];
        
        self.indicatorView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
        self.indicatorView.frame = rect;
        [self addSubview:self.indicatorView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

#pragma mark - protected methods

//利用数据刷新布局
/*
 *value1:消息内容
 *value2:发送时间
 *value3:是否本人
 *value4:头像url
 */
- (void)refreshFrameWithData:(MyDataObject *)item withWidth:(CGFloat)width
{
    CGRect rect = CGRectZero;
    CGSize size = [BubbleChatContentHeight getStringLength:item.value1];
    size.width = size.width > kChatLableMinHeight ? (size.width + 30.0) : (kChatLableMinHeight + 20.0);
    size.height = size.height > kChatLableMinHeight ? (size.height + kMarginTop + 5.0) : kChatLableMinHeight;
    
    if ([item.value3 isEqualToString:@"1"])    //left
    {
        rect = CGRectMake(10, 5, kChatHeaderImageHW, kChatHeaderImageHW);
        self.headerImage.frame = rect;
        [self.headerImage setImageWithURL:[NSURL URLWithString:item.value4] placeholderImage:[UIImage imageNamed:@"touxiang@2x.png"]];
        
        rect.origin.x = rect.origin.x + rect.size.width + 5.0;
        rect.size = CGSizeMake(size.width, size.height);
        self.chatView.frame = rect;
        self.chatView.locationFlag = 1;
        self.chatView.text = item.value1;
    }
    else                //right
    {
        rect = CGRectMake(width - 53, 5, kChatHeaderImageHW, kChatHeaderImageHW);
        self.headerImage.frame = rect;
        [self.headerImage setImageWithURL:[NSURL URLWithString:item.value4] placeholderImage:[UIImage imageNamed:@"touxiang@2x.png"]];
        
        rect.size = size;
        rect.origin.x = rect.origin.x - rect.size.width - 5.0;
        self.chatView.frame = rect;
        self.chatView.locationFlag = 2;
        self.chatView.text = item.value1;
        
        float oY = rect.size.height / 2.0 - 5;
        rect.origin.x = rect.origin.x - 35;
        rect = CGRectMake(rect.origin.x, oY, 30, 30);
        self.indicatorView.frame = rect;
    }
}

//展示旋转进度轮
- (void)showIndicatorView
{
    if (self.indicatorView && ![self.indicatorView isAnimating])
    {
        [self.indicatorView startAnimating];
    }
}

//影藏旋转进度轮
- (void)hideIndicatorView
{
    if (self.indicatorView && [self.indicatorView isAnimating])
    {
        [self.indicatorView stopAnimating];
    }
}

@end